package ui;


import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;

import ui.*;

import controller.*;


public class MainUI extends JFrame
{
	
	private static MainUI instance = null;
	private LoginMenu LoginUI;
	private EmployeeUI EmployeeUI;
	private HelpWindow HelpWindow;
	private CustomerUI CustomerUI;
	private ProductUI ProductUI;
	private RentsUI RentsUI;
	private SalesUI SalesUI;
	private StatisticsUI StatisticsUI;
	//private HelpWindow HelpWindow;
	private JPanel jCommerce;
	private JPanel jAdministration;
	private Border panelEdge;
	private JMenuBar jMenuBar;
	private JMenu jMenu;
	private JMenuItem signOut;
	private JMenuItem help;
	private JButton prodCtrl;
	private JButton statCtrl;
	private JButton saleCtrl;
	private JButton rentCtrl;
	private JButton userCtrl;
	private JButton EmpCtrl;
	private boolean isManager;
	
	private StatsController StsCtrlInstance;
	private SalesController SlsCtrlInstance;
	private RentController RntCtrlInstance;
	private ProductController PrdCtrlInstance;
	private EmployeeController EmpCtrlInstance;
	private CustomerController CusCtrlInstance;
	
	
	public static MainUI getInstance()
	{
		if(instance == null)
		{
			instance = new MainUI();
		}
		return instance;
	}
	public MainUI()
	{
		StsCtrlInstance = StsCtrlInstance.getInstance();
		SlsCtrlInstance = SlsCtrlInstance.getInstance();
		RntCtrlInstance = RntCtrlInstance.getInstance();
		PrdCtrlInstance = PrdCtrlInstance.getInstance();
		EmpCtrlInstance = EmpCtrlInstance.getInstance();
		CusCtrlInstance = CusCtrlInstance.getInstance();
		
		LoginUI = LoginMenu.getInstance();
		CustomerUI = CustomerUI.getInstance();
		EmployeeUI = EmployeeUI.getInstance();
		ProductUI = ProductUI.getInstance();
		SalesUI = SalesUI.getInstance();
		StatisticsUI = StatisticsUI.getInstance();
		HelpWindow = HelpWindow.getInstance();
		
		
		//HelpWindow = new HelpWindow();
		
		GridLayout gLayout = new GridLayout(0,2);
		GridLayout pLayout = new GridLayout(0,1);
		setLayout(gLayout);
		jMenuBar = new JMenuBar();
		
		
		jMenu = new JMenu("Options");
		jMenuBar.add(jMenu);
		
		signOut = new JMenuItem("Sign Out", new ImageIcon ("images/sign-out.png"));
		jMenu.add(signOut);
		event leave = new event();
		signOut.addActionListener(leave);
		
		help = new JMenuItem("Help", new ImageIcon ("images/Help.png"));
		jMenu.add(help);
		event2 helper = new event2();
		help.addActionListener(helper);
		
		setJMenuBar(jMenuBar);
		
		 JButton prodCtrl = new JButton("Products");
		 event3 products = new event3();
		 prodCtrl.addActionListener(products);
		 
		 JButton statCtrl = new JButton("Statistics");
		 event4 statistics = new event4();
		 statCtrl.addActionListener(statistics);
		 
		 JButton saleCtrl = new JButton("Sale");
		 event5 sales = new event5();
		 saleCtrl.addActionListener(sales);	
		 
		 JButton rentCtrl = new JButton("Rent");
		 event6 rents = new event6();
		 rentCtrl.addActionListener(rents);
		 
		 JButton userCtrl = new JButton("Customers");
		 event7 users = new event7();
		 userCtrl.addActionListener(users);
		 
		 JButton EmpCtrl = new JButton("Employees");
		 event8 employees = new event8();
		 EmpCtrl.addActionListener(employees);
		
		panelEdge = BorderFactory.createLineBorder(Color.black);
		
		jCommerce = new JPanel();
		jCommerce.setBorder(BorderFactory.createTitledBorder(panelEdge,"Commerce"));
		jCommerce.setLayout(gLayout);
		jCommerce.add(saleCtrl);
		jCommerce.add(rentCtrl);
		
		jAdministration = new JPanel();
		jAdministration.setBorder(BorderFactory.createTitledBorder(panelEdge, "Administration"));
		jAdministration.setLayout(pLayout);
		jAdministration.add(userCtrl);
		jAdministration.add(EmpCtrl);
		jAdministration.add(prodCtrl);
		jAdministration.add(statCtrl);
		
		getContentPane().add(jCommerce, BorderLayout.CENTER);
		getContentPane().add(jAdministration, BorderLayout.PAGE_END);
	
		setVisible(true);
		
	}
	
	public class event implements ActionListener
	{
		public void actionPerformed(ActionEvent leave)
		{
			LoginMenu.logOut();
			setVisible(false);
		}
	
	}
	public class event2 implements ActionListener
	{
		public void actionPerformed(ActionEvent helper)
		{
		//	HelpWindow.main(null);
			setVisible(false);
		}
	
	}
	public class event3 implements ActionListener
	{
		public void actionPerformed(ActionEvent products)
		{
			if(LoginUI.checkEmployee() == true)
			{
			try {
				ui.ProductUI.main(null);
				setVisible(false);
			} catch (Exception e) {
				e.printStackTrace();
			}
			}
		}
	
	}
	public class event4 implements ActionListener
	{
		public void actionPerformed(ActionEvent statistics)
		{
			if(LoginUI.checkEmployee() == true)
			{
			try {
				ui.ProductUI.main(null);
				setVisible(false);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
			}
		}
	
	}
	public class event5 implements ActionListener
	{
		public void actionPerformed(ActionEvent sales)
		{
			
			try {
				ui.SalesUI.main(null);
				setVisible(false);
			
			} catch(Exception e) {
				e.printStackTrace();
			}
			
		}
	
	}
	public class event6 implements ActionListener
	{
		public void actionPerformed(ActionEvent rents)
		{
			try {
				ui.RentsUI.main(null);
				setVisible(false);
				
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
	
	}
	public class event7 implements ActionListener
	{
		public void actionPerformed(ActionEvent users)
		{
			try {
				ui.CustomerUI.main(null);
				setVisible(false);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	
	}
	public class event8 implements ActionListener
	{
		public void actionPerformed(ActionEvent employees)
		{
			if(LoginUI.checkEmployee() == true)
			{
			try{
				ui.EmployeeUI.main(null);
				setVisible(false);
				
			} catch(Exception e)
			{
				e.printStackTrace();
			}
			}
		}
	
	}
	
	public static void main(String args[])
	{
		try {
           
        UIManager.setLookAndFeel(
            UIManager.getSystemLookAndFeelClassName());
    } 
    catch (UnsupportedLookAndFeelException e) {
       // handle exception
    }
    catch (ClassNotFoundException e) {
       // handle exception
    }
    catch (InstantiationException e) {
       // handle exception
    }
    catch (IllegalAccessException e) {
       // handle exception
    }
		MainUI gui = new MainUI();
		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		gui.pack();
		gui.setLocationRelativeTo(null);
		gui.setTitle("Main");
		gui.setResizable(false);
	//	gui.setVisible(true);
	}

	
	
}
